/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cv4j.app.slice;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Image;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.Texture;
import ohos.agp.utils.Color;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Size;

import com.cv4j.app.ResourceTable;
import com.cv4j.app.app.BaseAbilitySlice;
import com.cv4j.core.datamodel.CV4JImage;
import com.cv4j.core.datamodel.ImageProcessor;
import com.cv4j.core.hist.CalcHistogram;

/**
 * 直方图演示
 *
 * @since 2021-05-19
 */
public class HistogramDemoAbilitySlice extends BaseAbilitySlice {
    private Image image0;
    private Image image1;

    @Override
    protected int getUIContentLayoutId() {
        return ResourceTable.Layout_ability_histogram_demo;
    }

    @Override
    protected void initComponents(Intent intent) {
        image0 = (Image) findComponentById(ResourceTable.Id_image0);
        image1 = (Image) findComponentById(ResourceTable.Id_image1);

        image0.setPixelMap(ResourceTable.Media_test_hist2);
        CV4JImage cv4jImage = new CV4JImage(image0.getPixelMap());
        image1.setPixelMap(drawHist(cv4jImage.getProcessor(), new Paint()));
    }

    private PixelMap drawHist(ImageProcessor imageProcessor, Paint paint) {
        CalcHistogram calcHistogram = new CalcHistogram();
        int bins = 127;
        int[][] hist = new int[imageProcessor.getChannels()][bins];
        calcHistogram.calcRGBHist(imageProcessor, bins, hist, true);
        PixelMap.InitializationOptions options = new PixelMap.InitializationOptions();
        options.size = new Size(imageProcessor.getWidth(), imageProcessor.getHeight());
        options.pixelFormat = PixelFormat.ARGB_8888;
        PixelMap bm = PixelMap.create(options);
        Canvas canvas = new Canvas(new Texture(bm));
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.FILLANDSTROKE_STYLE);
        canvas.drawRect(0f, 0f, imageProcessor.getWidth(), imageProcessor.getHeight(), paint);
        float step = imageProcessor.getWidth() / 127f;
        int xoffset;
        int yoffset;
        int channels = imageProcessor.getChannels();
        int height = imageProcessor.getHeight();
        int[] colors = new int[]{Color.argb(77, 255, 0, 0), Color.argb(77, 0, 255, 0), Color.argb(77, 0, 0, 255)};
        for (int i = 0; i < channels; i++) {
            paint.setColor(new Color(colors[i]));
            for (int j = 0; j < bins; j++) {
                xoffset = (int) (j * step);
                yoffset = hist[i][j] * height / 255;
                canvas.drawRect(xoffset, height - yoffset, xoffset + step, height, paint);
            }
        }
        return bm;
    }
}
